<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <!-- <a-button-group :style="{ marginBottom: '10px' }">
        <router-link :to="{ name: 'GoodsList', query: { status: 1 } }"><a-button :type="status==1 ? 'primary' : ''">上架</a-button></router-link>
        <router-link :to="{ name: 'GoodsList', query: { status: 0 } }"><a-button :type="status==0 ? 'primary' : ''">下架</a-button></router-link>
      </a-button-group> -->
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="24">
            <a-form-item label="商品名称">
              <a-input v-model="queryParam.goodsName" placeholder="" />
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <span class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchBtn">查询</a-button>
              <a-button style="margin-left: 8px" @click="() => (this.queryParam = {})">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <div class="table-operator">
      <router-link :to="{ name: 'GoodsAdd' }"><a-button type="primary" icon="plus">新建</a-button></router-link>
    </div>

    <a-table
      ref="table"
      :row-key="
        (record) => {
          return record.goodsId
        }
      "
      :columns="columns"
      :data-source="data"
      :loading="loading"
      :pagination="pagination"
      @change="onChange"
    >
      <span slot="thumb" slot-scope="text, record">
        <img :src="record.thumb" v-if="record.thumb" style="width: 50px; heigt: 50px" />
      </span>
      <span slot="action" slot-scope="text, record">
        <template>
          <router-link :to="{ name: 'GoodsEdit', params: { id: record.goodsId } }">修改</router-link>
          <a-divider type="vertical" />
          <a @click="changeField('status', record.goodsId, 0)" v-if="record.status == 1" title="点击下架商品">下架</a>
          <a @click="changeField('status', record.goodsId, 1)" v-else title="点击上架商品">上架</a>
        </template>
      </span>
    </a-table>
  </a-card>
</template>

<script>
import modal from 'ant-design-vue/es/modal'

const columns = [
  {
    title: 'ID',
    dataIndex: 'goodsId'
  },
  {
    title: '商品名称',
    dataIndex: 'goodsName'
  },
  {
    title: '商品类型',
    dataIndex: 'categoryName'
  },
  {
    title: '所需积分',
    dataIndex: 'credit'
  },
  {
    title: '商品库存',
    dataIndex: 'stock'
  },
  {
    title: '缩略图',
    dataIndex: 'thumb',
    scopedSlots: { customRender: 'thumb' }
  },
  {
    title: '点击数',
    dataIndex: 'clickCount'
  },
  {
    title: '创建时间',
    dataIndex: 'createdAt'
  },
  {
    title: '操作',
    key: 'action',
    scopedSlots: { customRender: 'action' }
  }
]

export default {
  name: 'GoodsList',
  data() {
    return {
      status: typeof this.$route.query.status !== 'undefined' ? parseInt(this.$route.query.status) : 1,
      columns,
      loading: false,
      data: [],
      selectedRowKeys: [],
      pagination: {
        showQuickJumper: true,
        showSizeChanger: true,
        defaultCurrent: 1, // 默认当前页数
        defaultPageSize: 10, // 默认当前页显示数据的大小
        total: 0, // 总数，必须先有
        pageSizeOptions: ['10', '20', '30', '50', '100'],
        showTotal: (total, range) => {
          return (
            <div>
              共 <span>{total}</span> 条
            </div>
          )
        },
        onShowSizeChange: (current, pageSize) => {
          this.pageSize = pageSize
        }
      },
      queryParam: {
        categoryId: '',
        goodsName: '',
        brandId: ''
      },
      categoryLists: [],
      brandLists: []
    }
  },
  mounted() {
    // 获取列表
    this.fetch()
    // 获取分类列表
    // this.getCategoryList()
    // 获取品牌列表
    // this.getBrandList()
  },
  methods: {
    // 获取分类列表
    getCategoryList() {
      this.$common
        .handleGet('/goods/category/lists', { page: 1, pagesize: 1000 })
        .then((res) => {
          if (res.code === 200) {
            res.data.lists.forEach((r) => {
              this.categoryLists.push({
                categoryId: r.categoryId,
                categoryName: r.categoryName
              })
            })
          }
        })
        .catch((err) => {
          this.$message.error(err)
        })
    },
    // 获取品牌列表
    getBrandList() {
      this.$common
        .handleGet('/goods/brand/lists', { page: 1, pagesize: 1000 })
        .then((res) => {
          if (res.code === 200) {
            res.data.lists.forEach((r) => {
              this.brandLists.push({
                brandId: r.brandId,
                brandName: r.brandName
              })
            })
          }
        })
        .catch((err) => {
          this.$message.error(err)
        })
    },
    // 改变每页数量时更新显示
    onChange(pagination) {
      this.pagination.defaultCurrent = pagination.current
      this.pagination.defaultPageSize = pagination.pageSize
      this.fetch()
    },
    // 搜索
    searchBtn() {
      this.pagination.defaultCurrent = 1
      this.fetch()
    },
    // 动态获取列表
    fetch() {
      this.selectedRowKeys = []
      this.loading = true
      const params = {
        page: this.pagination.defaultCurrent,
        pagesize: this.pagination.defaultPageSize
        // status: this.status
        // goodsName: this.queryParam.goodsName
      }
      this.$common
        .fetchList('/goods/lists', Object.assign({}, params, this.queryParam))
        .then((data) => {
          this.loading = false
          if (data.code === 200) {
            const result = data.data
            const pagination = { ...this.pagination }
            pagination.total = result.totalInfo.total_items
            this.data = result.lists
            this.pagination = pagination
          }
        })
        .catch(() => {
          this.$notification.error({
            message: '错误',
            description: '请求信息失败，请重试'
          })
        })
    },
    onSelectChange(selectedRowKeys) {
      console.log('selectedRowKeys changed: ', selectedRowKeys)
      this.selectedRowKeys = selectedRowKeys
    },
    // 状态、推荐
    changeField(field, id, value) {
      if (field && id && value !== '') {
        var param = {}
        var msg = '下架'
        var url = '/goods/disable/'
        value = parseInt(value)
        if (value === 1) {
          msg = '上架'
          url = '/goods/enable/'
        }
        const _this = this
        modal.confirm({
          title: `确定要${msg}吗?`,
          onOk() {
            _this.$common.changeField(url + id, param).then((data) => {
              if (data.code === 200) {
                _this.$message.success(data.msg)
                _this.fetch()
              } else {
                _this.$message.error(data.msg)
              }
            })
          }
        })
      } else {
        this.$message.error('参数错误')
      }
    }
  }
}
</script>
